using UnityEngine;
using System;
using System.Text;



/// <summary>
/// 字符串扩展类
/// </summary>
namespace NuoYan.Extension
{
    public static class StringExtension
    {
        /// <summary>
        /// 增加颜色的亮度
        /// </summary>
        /// <param name="color">颜色</param>
        /// <param name="factor">强度</param>
        /// <returns></returns>
        public static Color IncreaseBrightness(Color color, float factor)
        {
            float r = Mathf.Min(color.r * factor, 1.0f);
            float g = Mathf.Min(color.g * factor, 1.0f);
            float b = Mathf.Min(color.b * factor, 1.0f);
            return new Color(r, g, b, color.a);
        }
        /// <summary>
        /// 将字符串以html的标签形式显示，并设置颜色
        /// </summary>
        /// <param name="str"></param>
        /// <param name="color"></param>
        /// <param name="intensity"></param>
        /// <returns></returns>
        public static string ColorString(this string str, Color color, float intensity = 1f)
        {
            color = IncreaseBrightness(color, intensity);
            return $"<color=#{ColorUtility.ToHtmlStringRGB(color)}>{str}</color>";
        }

        public static string ColorString(this string str, string color)
        {
            return $"<color={color}>{str}</color>";
        }
        /// <summary>
        /// 将字符串中的某一串字符以html的标签形式显示，并设置颜色
        /// </summary>
        /// <param name="str"></param>
        /// <param name="c">字符串中的指定字符</param>
        /// <param name="color"></param>
        /// <param name="intensity"></param>
        /// <returns></returns>
        public static string ColorString(this string str, string c, Color color, int intensity = 1)
        {
            if (str.Contains(c))
            {
                return str.Replace(c.ToString(), c.ColorString(color, intensity));
            }
            else
            {
                return str;
            }
        }
        /// <summary>
        /// 裁剪字符串
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="start">开始字符</param>
        /// <param name="length">长度</param>
        public static string Substring(this string str, string start, int length)
        {
            return str.Substring(str.IndexOf(start), length);
        }
        /// <summary>
        /// 裁剪字符串
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="start">开始字符</param>
        /// <param name="length">截至字符</param>
        public static string Substring(this string str, string start, string end)
        {
            return str.Substring(str.IndexOf(start), str.IndexOf(end));
        }
        /// <summary>
        /// 清除字符串中的颜色
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ClearStringColor(this string str)
        {
            if (str.Contains("#"))
            {
                var color = str.Substring("#", 8);  // #ffffff>
                return str.Replace("<color=", "").Replace("</color>", "").Replace(color, "");
            }
            return str;
        }
        /// <summary>
        /// 将字符串以html的标签形式显示，并设置下划线和链接
        /// </summary>
        /// <param name="str"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string LinkString(this string str, string id)
        {
            return $"<link={id}><u>{str}</u></link>";
        }
        /// <summary>
        /// 忽略空格
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string IgnoreWhitespace(this string str)
        {
            return System.Text.RegularExpressions.Regex.Replace(str, @"\s+", "");
        }

    }

}

